package mysql

import (
	"shuqi-novel/userrpc/userrpc"

	"gorm.io/gorm"
)

// 小说

type Novel struct {
	gorm.Model
	NovelTitle   string `gorm:"type:varchar(30);not null;comment:小说标题"`
	NovelContent string `gorm:"type:varchar(30);not null;comment:小说简介"`
	NovelStyle   string `gorm:"type:varchar(30);not null;comment:小说分类"`
	UserId       int    `gorm:"type:int;not null;comment:用户ID"`
}

// 小说添加

func (n *Novel) NovelAdd(db *gorm.DB) error {
	return db.Debug().Create(n).Error
}

// 小说修改

func (n *Novel) NovelUpdate(db *gorm.DB) error {
	return db.Debug().Updates(n).Error
}

// 小说列表

func NovelList(db *gorm.DB, in *userrpc.NovelListRequest) []*userrpc.NovelList {
	/*
		select
		users.id as UserId,
		users.username as Username,
		users.`password` as Password,
		novels.novel_title as NovelTitle,
		novels.novel_content as NovelContent,
		novels.novel_style as NovelStyle
		from users
		left join novels on novels.user_id = users.id
	*/
	var list []*userrpc.NovelList

	db.Model(&User{}).
		Select(
			"users.id as UserId",
			"users.username as Username",
			"users.`password` as Password",
			"novels.novel_title as NovelTitle",
			"novels.novel_content as NovelContent",
			"novels.novel_style as NovelStyle",
		).
		Joins("left join novels on novels.user_id = users.id").
		Debug().Scopes(Paginate(int(in.Page), int(in.Size))).Find(&list)

	return list
}
